package com.start.dataSource.controller;



import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.common.api.Result;
import com.start.common.dataRule.DataQueryConstructor;
import com.start.common.exceptions.StartBusException;
import com.start.core.annotations.RegisterMenu;
import com.start.core.annotations.RegisterPermission;
import com.start.dataSource.entity.StartSourceDataTable;
import com.start.dataSource.service.StartDataTableService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "数据表管理")
@RestController
@RequestMapping("/start/dataSource/table")
@RegisterMenu(value = "数据表管理", systemCode = "start:dataSource:table", component = "views/start/dataSource/table", path = "/mappers/start/dataSource/table")
public class StartDataTableController {
    @Autowired
    private StartDataTableService sysDataTableService;
    @Autowired
    private DynamicDataSourceProperties dynamicDataSourceProperties;

    @Operation(summary = "分页查询数据表")
    @GetMapping("/page")
    public Result<?> page(Page page, String dataSourcesKey) {
        if (StringUtils.isEmpty(dataSourcesKey)) {
            dataSourcesKey = dynamicDataSourceProperties.getPrimary();
        }
        Map<String, DataSourceProperty> datasource = dynamicDataSourceProperties.getDatasource();
        if (sysDataTableService.hasDataSource(dataSourcesKey)) {
            throw new StartBusException("该数据源不存在或未启用" + dataSourcesKey + ",请先添加数据并启用");
        }
        QueryWrapper<StartSourceDataTable> queryWrapper = DataQueryConstructor.createEntity(StartSourceDataTable.class);
        return Result.ok(sysDataTableService.pageSystemTable(page, queryWrapper, dataSourcesKey));
    }

    @PostMapping("/add")
    @PreAuthorize("@pas.hasPermission('start:dataSource:table:add')")
    @RegisterPermission(value = "start:dataSource:table:add", name = "添加")
    public Result<?> add(@RequestBody StartSourceDataTable entity) {
        sysDataTableService.createTable(entity);
        return Result.ok();
    }

    @PutMapping("/updateById")
    @PreAuthorize("@pas.hasPermission('start:dataSource:table:edit')")
    @RegisterPermission(value = "start:dataSource:table:edit", name = "修改")
    public Result<?> updateById(@RequestBody StartSourceDataTable entity) {
        sysDataTableService.updateById(entity);
        return Result.ok();
    }

    @GetMapping("/getById/{id}")
    @PreAuthorize("@pas.hasPermission('start:dataSource:table:info')")
    @RegisterPermission(value = "start:dataSource:table:info", name = "查看")
    public Result<?> getById(@PathVariable("id") String id) {
        return Result.ok(sysDataTableService.getTableById(id, true));
    }


    @DeleteMapping("/deleteById/{id}")
    @PreAuthorize("@pas.hasPermission('start:dataSource:table:delete')")
    public Result<?> deleteById(@PathVariable("id") String id) {
        sysDataTableService.removeTableById(id);
        return Result.ok();
    }

    @Operation(summary = "同步数据表")
    @GetMapping("/synchronizationTable")
    @PreAuthorize("@pas.hasPermission('start:dataSource:table:createTable')")
    public Result<?> synchronizationTable(String dataSourcesKey, String tableName) {
        if (sysDataTableService.hasDataSource(dataSourcesKey)) {
            throw new StartBusException("该数据源不存在或未启用:" + dataSourcesKey + ",请先添加数据并启用");
        }
        sysDataTableService.synchronizationTable(dataSourcesKey, tableName);
        return Result.ok();
    }

    @Operation(summary = "设计数据表")
    @GetMapping("/getDesignConfig/{dataSourcesKey}/{tableName}")
    public Result<?> getDesignConfig(@PathVariable("dataSourcesKey") String dataSourcesKey, @PathVariable("tableName") String tableName) {
        return Result.ok(sysDataTableService.getDesignConfig(dataSourcesKey, tableName));
    }

    @Operation(summary = "提交设计的数据表")
    @GetMapping("/submitDesign/{dataSourcesKey}/{tableName}")
    public Result<?> submitDesign(
            @PathVariable("dataSourcesKey") String dataSourcesKey,
            @PathVariable("tableName") String tableName) {
        return Result.ok();
    }

}
